package com.journaldev.spring.util;

import java.beans.PropertyEditorSupport;

import com.journaldev.spring.model.PersonalOffice;
import com.journaldev.spring.service.PersonalOfficeService;

public class SimpleOfficeEditor extends PropertyEditorSupport {
	
	private PersonalOfficeService officeService;
	
	public SimpleOfficeEditor(PersonalOfficeService officeSv) {
		this.officeService = officeSv;
	}
	
	@Override
    public void setAsText(String text) throws IllegalArgumentException {
    	PersonalOffice office = null;
        try {
            Integer id = Integer.parseInt(text);
            office = officeService.getOfficeById(id);
            System.out.println("Office name:" + office.getOfficeName());
        } catch (NumberFormatException ex) {
            System.out.println("District will be null");
        }
        setValue(office);
    }
}
